package metaphor.persistence.model;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;

import java.io.Serializable;

/**
 * Gender
 * @author Mark Ashworth
 * @version 1.0.0
 * @since 1.0.0
 */
public class Gender implements Serializable {

    private Long id = null;

    /* The name of the gender */
    private String name;

    /* Whether the gender is enabled */
    private boolean enabled = true;

    /* Empty */
    public static final Gender EMPTY = new Gender();

    /** Male gender */
    public static final Gender MALE = new Gender("MALE");

    /** Female gender */
    public static final Gender FEMALE = new Gender("FEMALE");

    /**
     * Default constructor
     */
	public Gender() {
		this(null);
	}

    /**
     * Constructs the gender with it's name
     * @param name The gender name
     */
	public Gender(String name) {
		super();
        this.name = name;
	}

    /**
     * The unique id of the gender
     * @return Long
     */
    public Long getId() {
		return this.id;
	}

    /**
     * The name of the gender
     * @return String
     */
    public String getName() {
		return this.name;
	}

    /**
     * Whether the gender is enabled
     * @return boolean
     */
    public boolean isEnabled() {
        return this.enabled;
    }

    /**
     * Returns the gender name as the String representation
     * @return String
     */
    @Override
    public String toString() {
        return getName();
    }

    /**
     * &lt;gender class="xperiment.metaphor.model.people.Gender" name="MALE"/&gt;
     * @return String
     */
    public String toXML() {
        return "<gender class='" + getClass().getName()
                + "' id='" + getId()
                + "' name='" + getName()
                + "' enabled='" + isEnabled()
                + "' />";
    }

    /**
     * Calculates equality based upon the name of the gender
     * @param obj The other object
     * @return boolean
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) { return false; }
        if (obj == this) { return true; }
        if (obj.getClass() != getClass()) {
            return false;
        }
        Gender rhs = (Gender) obj;
        return new EqualsBuilder()
            .append(this.name, rhs.getName())
            .isEquals();
    }

    /**
     * Calculates the hash code based upon the name of the gender
     * @return int
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 39).append(this.name).toHashCode();
    }
}